package com.wcity.manage.service.community.partybuilding.impl;

import com.wcity.manage.common.enums.SaveImageType;
import com.wcity.manage.common.vo.community.partybuilding.PartyBuildingVo;
import com.wcity.manage.dao.mapper.community.partybuilding.PartyBuildingMapper;
import com.wcity.manage.dao.model.community.image.ImageUrl;
import com.wcity.manage.dao.model.community.partybuilding.PartyBuilding;
import com.wcity.manage.service.community.image.ImageService;
import com.wcity.manage.service.community.partybuilding.PartyBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 党政建设
 * Created by hgg on 2016/12/20.
 */
@Service
public class PartyBuildingServiceImpl implements PartyBuildingService {

    @Autowired
    private PartyBuildingMapper partyBuildingMapper;
    @Autowired
    private ImageService imageService;

    /**
     * 党政建设列表
     * @param queryMap
     * @return
     */
    @Override
    public List<PartyBuildingVo> partyBuildingPageList( Map<String, Object> queryMap ) {

        return partyBuildingMapper.partyBuildingPageList( queryMap );
    }

    /**
     * 删除党政建设
     * @param deleteMap
     * @return
     */
    @Override
    public Integer deletePartyBuilding( Map<String, Object> deleteMap ) {

        return partyBuildingMapper.deletePartyBuilding( deleteMap );
    }

    /**
     * 新增党政建设
     * @param partyBuilding
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer saveAddPartyBuilding( PartyBuilding partyBuilding,String imgUrl,String blockIdStr ) {

        //【1】生成父级党政建设
        Integer parentId = 0;
        String[] blockIds = blockIdStr.split( "," );
        partyBuilding.setBlockId( Integer.parseInt( blockIds[0] ) );
        partyBuilding.setPid( 0 );
        Integer saveNum = partyBuildingMapper.saveAddPartyBuilding( partyBuilding );
        if( saveNum == 0 ){
            throw new RuntimeException("新增党政建设失败");
        }
        parentId = partyBuilding.getId();

        //【2】保存关联封面图片
        ImageUrl image = new ImageUrl( null,imgUrl,null,parentId,SaveImageType.COMMUNITY_PARTY_BUILDING );
        Integer saveImageNum = imageService.saveAddImage( image );
        if( saveImageNum == 0 ){
            throw new RuntimeException( "保存党政建设封面图片失败" );
        }

        //【3】执行保存党政建设
        saveAddPartyBuilding( partyBuilding, blockIds, parentId,imgUrl );
        return 1;
    }

    /**
     * 更新党政建设
     * @param partyBuilding
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer saveUpdatePartyBuilding( PartyBuilding partyBuilding,String imgUrl,String blockIdStr ) {

        Integer parentId = partyBuilding.getId();
        String[] blockIds = blockIdStr.split( "," );
        //【1】删除以前党政建设封面
        Map<String,Object> deleteMap = new HashMap<>();
        deleteMap.put( "referId", partyBuilding.getId() );
        deleteMap.put( "saveType", SaveImageType.COMMUNITY_PARTY_BUILDING );
        imageService.deleteImage( deleteMap );

        //【2】删除以前的小区概况
//        Map<String,Object> deletePartyMap = new HashMap<>();
//        deletePartyMap.put( "id",partyBuilding.getId() );
//        deletePartyMap.put( "userId",partyBuilding.getUserId() );
//        Integer deleteNum = partyBuildingMapper.deletePartyBuilding( deletePartyMap );
//        if( deleteNum == 0 ){
//            throw new RuntimeException( "删除党政建设失败" );
//        }

        //【3】执行保存党政建设封面
        ImageUrl image = new ImageUrl( null,imgUrl,null,partyBuilding.getId(),SaveImageType.COMMUNITY_PARTY_BUILDING );
        Integer saveImageNum = imageService.saveAddImage( image );
        if( saveImageNum == 0 ){
            throw new RuntimeException( "保存党政建设封面图片失败" );
        }

        //【4】执行更新党政建设概况
        partyBuilding.setBlockId( Integer.parseInt( blockIds[0] ) );
        Integer updateNum = partyBuildingMapper.saveUpdatePartyBuilding( partyBuilding );
        if( updateNum == 0 ){
            throw new RuntimeException( "更新党政建设失败 ");
        }

        //【5】执行新增党政建设
//        saveAddPartyBuilding(partyBuilding, blockIds, parentId,imgUrl);
        return 1;
    }

    /**
     * 党政建设详情
     * @param detailMap
     * @return
     */
    @Override
    public PartyBuildingVo partyBuildingDetail( Map<String, Object> detailMap ) {

        return partyBuildingMapper.partyBuildingDetail( detailMap );
    }

    /**
     * 党政建设总数量
     * @param queryMap
     * @return
     */
    @Override
    public Integer totalPartyBuilding( Map<String, Object> queryMap ) {

        return partyBuildingMapper.totalPartyBuilding( queryMap );
    }

    /**
     * 党政建设列表非分页
     * @param queryMap
     * @return
     */
    @Override
    public List<PartyBuildingVo> partyBuildingList( Map<String, Object> queryMap ) {

        return partyBuildingMapper.partyBuildingList( queryMap );
    }

    /**
     * 执行新增党政建设
     * @param partyBuilding
     * @param blockIds
     * @param parentId
     */
    private void saveAddPartyBuilding( PartyBuilding partyBuilding, String[] blockIds,Integer parentId,String imgUrl ){

        for ( String id : blockIds ){
            partyBuilding.setBlockId( Integer.parseInt( id ) );
            //【1】执行保存
            partyBuilding.setPid( parentId );
            Integer saveNum = partyBuildingMapper.saveAddPartyBuilding( partyBuilding );
            if( saveNum == 0 ){
                throw new RuntimeException("保存党政建设失败");
            }

            //【2】保存关联封面图片
            ImageUrl image = new ImageUrl( null,imgUrl,null,partyBuilding.getId(),SaveImageType.COMMUNITY_PARTY_BUILDING );
            Integer saveImageNum = imageService.saveAddImage( image );
            if( saveImageNum == 0 ){
                throw new RuntimeException( "保存党政建设封面图片失败" );
            }
        }
    }

    /**
     * 查询小区ID拼接（1,2,3）
     * @param queryMap
     * @return
     */
    public String blockIds( Map<String,Object> queryMap ){

        return partyBuildingMapper.blockIds( queryMap );
    }
}
